package com.lld.controller;

import com.lld.entity.Thing;
import com.lld.entity.User;
import com.lld.service.ThingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: memo
 * @description: 备忘录控制器
 * @author: lld
 * @create: 2020-10-09 19:54
 **/
@RestController
@RequestMapping("/thing")
public class ThingController {
    @Autowired
    private ThingService thingService;

    @RequestMapping("/getAllThing")
    public List<Thing> getAllThing(HttpSession session) {
        User user = (User) session.getAttribute("user");
        return thingService.queryThingById(user.getId());
    }

    @RequestMapping("/addThing")
    public Map<String, Object> addThing(String text, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        Thing thing = new Thing();
        thing.setText(text);
        thing.setCtime(new Date());
        User user = (User) session.getAttribute("user");
        thing.setUid(user.getId());
        thingService.insertThing(thing);
        return map;
    }

    @RequestMapping("/deleteThing")
    public Map<String, Object> deleteThing(Integer id) {
        Map<String, Object> map = new HashMap<>();
        thingService.removeThing(id);
        return map;
    }
}
